#include "touch.h"
#include "sw_i2c.h"

static uint8_t touch_trigger;

TouchDataType gTouchPoint;

/* SDA、SCL引脚设置为输出上拉模式 */
void myGPIO_INIT(void)
{

}
/* SDA引脚初始化为输出上拉高电平 */
void mySDA_OUT_H(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    HAL_GPIO_WritePin(T_SDA_GPIO_Port, T_SDA_Pin, GPIO_PIN_SET);
    GPIO_InitStruct.Pin = T_SDA_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    HAL_GPIO_Init(T_SDA_GPIO_Port, &GPIO_InitStruct);
}
/* SDA引脚初始化为输出上拉低电平 */
void mySDA_OUT_L(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    HAL_GPIO_WritePin(T_SDA_GPIO_Port, T_SDA_Pin, GPIO_PIN_RESET);
    GPIO_InitStruct.Pin = T_SDA_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    HAL_GPIO_Init(T_SDA_GPIO_Port, &GPIO_InitStruct);
}
/* SDA引脚设置为输入模式 */
void mySDA_IN(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    GPIO_InitStruct.Pin = T_SDA_Pin;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_PULLDOWN;
    HAL_GPIO_Init(T_SDA_GPIO_Port, &GPIO_InitStruct);
}
/* 读取SDA引脚电平 */
SW_I2C_SDA_VALUE myREAD_SDA(void)
{
    return (SW_I2C_SDA_VALUE)HAL_GPIO_ReadPin(T_SDA_GPIO_Port, T_SDA_Pin);
}
/* SDA引脚输出高电平 */
void mySDA_H(void)
{
    HAL_GPIO_WritePin(T_SDA_GPIO_Port, T_SDA_Pin, GPIO_PIN_SET);
}
/* SDA引脚输出低电平 */
void mySDA_L(void)
{
    HAL_GPIO_WritePin(T_SDA_GPIO_Port, T_SDA_Pin, GPIO_PIN_RESET);
}
/* SCL引脚输出高电平 */
void mySCL_H(void)
{
    HAL_GPIO_WritePin(T_SCL_GPIO_Port, T_SCL_Pin, GPIO_PIN_SET);
}
/* SCL引脚输出低电平 */
void mySCL_L(void)
{
    HAL_GPIO_WritePin(T_SCL_GPIO_Port, T_SCL_Pin, GPIO_PIN_RESET);
}
/* 5us延时 */
void delay5us(void)
{
    volatile uint32 i = 750;
    while(i--);
}
/* 2us延时 */
void delay2us(void)
{
    volatile uint32 i = 300;
    while(i--);
}

uint8_t touchId[4];
void touch_init(void)
{
    SW_I2C_HOOKS iic;
    uint8_t txBuf[8];

    // CS EN
    HAL_GPIO_WritePin(T_CS_GPIO_Port, T_CS_Pin, GPIO_PIN_SET);

    iic.DELAY_2US = delay2us;
    iic.DELAY_5US = delay5us;
    iic.GPIO_INIT = myGPIO_INIT;
    iic.READ_SDA = myREAD_SDA;
    iic.SCL_H = mySCL_H;
    iic.SCL_L = mySCL_L;
    iic.SDA_H = mySDA_H;
    iic.SDA_IN = mySDA_IN;
    iic.SDA_L = mySDA_L;
    iic.SDA_OUT_H = mySDA_OUT_H;
    iic.SDA_OUT_L = mySDA_OUT_L;
    sw_i2c_regist_init(&iic);

    // HAL_Delay(10);
    {volatile uint32_t i = 150000; while(i--);}

    // Get ID
    txBuf[0] = 0x81;
    txBuf[1] = 0x40;
    sw_i2c_write_bytes(0x5D, txBuf, 2);
    sw_i2c_read_bytes(0x5D, touchId, 4);

     txBuf[0] = 0x81;
     txBuf[1] = 0x4E;
     txBuf[2] = 0x00;
     sw_i2c_write_bytes(0x5D, txBuf, 3);
}

void touch_int(void)
{
    touch_trigger = 1;
}


void touch_scan(void)
{
    // static uint32_t cnt_10ms;
    uint8_t txBuf[8] = {0x81, 0x4E, 0x00};
    uint8_t rxBuf[40];
    uint8_t i = 0;

    if(0 == touch_trigger)
        return;

    // 10ms
    // if(HAL_GetTick() < cnt_10ms + 10)
    //     return;
    // cnt_10ms = HAL_GetTick();

    // Read the coordinates of the touch point.
    sw_i2c_write_bytes(0x5D, txBuf, 2);
    sw_i2c_read_bytes(0x5D, rxBuf, 1);

    gTouchPoint.buf_status = rxBuf[0] & 0x80;
    gTouchPoint.touch_num = rxBuf[0] & 0x0F;

    if(!gTouchPoint.touch_num)
    {
        touch_trigger = 0;
        // Clear status. Very important, do not delete.
        sw_i2c_write_bytes(0x5D, txBuf, 3);
        return;
    }

    // Read the coordinates of the touch point.
    sw_i2c_write_bytes(0x5D, txBuf, 2);
    sw_i2c_read_bytes(0x5D, rxBuf, gTouchPoint.touch_num*8);
    for(i = 0; i < gTouchPoint.touch_num && i < MAX_TOUCH_NUM; i++)
    {
        gTouchPoint.tp[i].trackId = rxBuf[8*i+ 1];
        gTouchPoint.tp[i].xPos = (uint16_t)(rxBuf[8*i + 3] << 8) | rxBuf[8*i + 2];
        gTouchPoint.tp[i].yPos = (uint16_t)(rxBuf[8*i + 5] << 8) | rxBuf[8*i + 4];
        gTouchPoint.tp[i].pointSize = (uint16_t)(rxBuf[8*i + 7] << 8) | rxBuf[8*i + 6];
    }

    // Clear status. Very important, do not delete.
    sw_i2c_write_bytes(0x5D, txBuf, 3);
}


